define('pc/js/mod/write/vue-write-list', [
    'components/VuePage/vue-page',
    'utils/Time',
    'utils/helpers',
    'utils/fetchEnhancer'
], (VuePage, Time, helpers, fetch) => {

const List = {
    data: function() {
        return {
            current: 1,
            total: 0,
            pageSize: 10,
            list: null
        };
    },
    methods: {
        formatTime: function(time) {
            return Time.format('y-m-d h:i', time);
        },
        getImagePath: function(path) {
            return helpers.uploadPath(path);
        },

        fetchList: function(page) {
            var _self = this;

            if(!this.strUid) {
                return;
            }

            fetch('/api/post/list?review=1&page='+ page +'&uid=' + this.strUid).then((json) => {
                if(0 === json.status) {
                    _self.current = json.data.current;
                    _self.list = json.data.data;
                    _self.total = json.data.total;
                    _self.pageSize = json.data.page_size;
                }
            });
        },
        deletePost: function(id) {
            var _self = this;
            XUI.dialog.confirm('确定要删除吗？', '提示', (f) => {
                if(1 !== f) {
                    return;
                }

                // 删除接口
                fetch('/api/post/delete', {
                    method: 'POST',
                    body: 'id=' + id
                }).then((json) => {
                    if(0 === json.status) {
                        _self.fetchList(_self.current);
                    }
                });
            });
        },
        handlePageChange: function(p) {
            this.fetchList(p);
        },
        handlePageClick: function(e) {
            var t = e.target;
            var role = t.dataset.role;

            if('del' === role) {
                this.deletePost(t.dataset.id);
                return;
            }
        }
    },
    components: {
        'vue-page': VuePage
    },
    created: function() {
        this.strUid = USER_SDK.getInstance().getStrUid();
    },
    mounted: function() {
        this.fetchList(this.current);
    },
    template:
`<div @click="handlePageClick">
<table class="x-table" v-if="list">
    <thead><tr><th>id</th><th width="300">标题</th><th>封面</th><th>浏览量</th><th>发布时间</th><th>状态</th><th>操作</th></tr></thead>
    <tbody>
    <tr v-for="item in list">
        <td>{{ item.id }}</td>
        <td><a target="_blank" :href="'/posts/'+item.id">{{ item.title }}</a></td>
        <td><img width="100" :src="getImagePath(item.thumb)"></td>
        <td>{{ item.views }}</td>
        <td>{{ this.formatTime(item.post_time * 1000) }}</td>
        <td>
            <span v-if="item.status==='1'">正常</span>
            <span v-if="item.status==='0'" class="x-color-red">审核中</span>
        </td>
        <td>
            <a data-role="edit" :href="'#/write/edit/' + item.id">修改</a>
            <span class="x-divider"></span>
            <a data-role="del" :data-id="item.id">删除</a>
        </td>
    </tr>
    </tbody>
</table>
<div>
    <vue-page :totalRecords="total" :pageSize="pageSize" :onChange="handlePageChange"></vue-page>
</div>
</div>`
};
return List;

});
